<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function ajax_post(option) {
            let xhr = new XMLHttpRequest()
            xhr.open('post', option.url)
            xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
            xhr.onreadystatechange = () => {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200) {
                        option.success && option.success(xhr.responseText)
                    } else {
                        option.error && option.error()
                    }
                }
            }
            if (option.params) {
                let str = ''
                for (let key in option.params) {
                    str += `${key}=${option.params[key]}`
                    str += '&'
                }
                str = str.slice(0, -1)
                xhr.send(str)
            } else {
                xhr.send()
            }
        }
        ajax_post({
                url: 'http://localhost:8888/test/fourth',
                params: { name: '张三', age: 18 },
                success(data) {
                    console.log(JSON.parse(data))
                }
            })

    </script>
</body>

</html>